package com.sychip.fhc.app.ble


import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.fhc.base.EventObserver
import com.fhc.base.FhcFlowEventBus
import com.fhc.base.FlowEventBusData
import com.fhc.view.utils.SvgIconRotate
import com.sychip.fhc.app.todo.R
import com.sychip.fhc.app.wsn.common.EventDataBleNfc
import com.sychip.fhc.base.BaseAppViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import timber.log.Timber


@Preview
@Composable
fun BleNFCControl( modifier: Modifier = Modifier,viewModel: BleNfcViewModel = hiltViewModel(), ){
//    val appViewModel = MainApplication.instance().getAppViewModel()
    val appViewModel = hiltViewModel<BaseAppViewModel>()
    val nfcBleStatusState = viewModel.nfcStatus.collectAsStateWithLifecycle()
    val splashTxt = remember { mutableStateOf("\n ${nfcBleStatusState.value.info}  \n ${nfcBleStatusState.value.bleMac}") }
    Timber.i("splashTxt  >>>>>>  ${splashTxt.value}")
    EventObserver(onCreate = {
        CoroutineScope(Dispatchers.Default).launch {
            FhcFlowEventBus.subscribe<FlowEventBusData> { event->
                CoroutineScope(Dispatchers.Main).launch {
                    if(event.dataObj is EventDataBleNfc){
                        val  nfcStatus = event.dataObj as EventDataBleNfc
                        if(nfcStatus.state == 2 || nfcStatus.state == -1)  splashTxt.value ="\n${nfcStatus.info}  \nTime:${nfcStatus.connectedTime}(ms)"
                        else if(nfcStatus.state == 0 || nfcStatus.state == 1)  splashTxt.value ="\n${nfcStatus.info}  \n${nfcStatus.bleMac}"
                        Timber.i("splashTxt:   ${splashTxt.value}")
                    }
                }
            }
        }
    })


//    if(nfcStatus.state == 2 || nfcStatus.state == -1)  splashTxt.value ="\n${nfcStatus.info}  \nTime:${nfcStatus.connectedTime}(ms)"
//    else if(nfcStatus.state == 0 || nfcStatus.state == 1)  splashTxt.value ="\n${nfcStatus.info}  \n"

    val colorOn = colorScheme.primary
    Box(modifier = modifier) {
        Box(modifier = Modifier.fillMaxSize().background(color = Color.White)) {
            Column(modifier = Modifier.fillMaxSize().wrapContentSize().align(Alignment.Center)) {
                Image(
                    painter = painterResource(id = R.drawable.ic_nfc),
                    colorFilter = ColorFilter.tint(colorOn),
                    contentDescription = null,
                    modifier = Modifier.fillMaxWidth().height(80.dp).padding(10.dp),
                )
                Image(
                    painter = painterResource(id = R.drawable.ic_ble),
                    colorFilter = ColorFilter.tint(colorOn),
                    contentDescription = null,
                    modifier = Modifier.fillMaxWidth().height(80.dp).padding(10.dp),
                )
                if(nfcBleStatusState.value.state == 1){
                    SvgIconRotate(
                        path = "svg/loading.svg",
                        tint = Color.Blue,
                        modifier = Modifier.fillMaxWidth().size(24.dp)
                    )
                }    else  if(nfcBleStatusState.value.state == 2){
                    Image(
                        painter = painterResource(id = R.drawable.ic_warning),
                        colorFilter = ColorFilter.tint(Color.Green),
                        contentDescription = null,
                        modifier = Modifier.fillMaxWidth().height(80.dp).padding(10.dp),
                    )
                }   else  if(nfcBleStatusState.value.state == -1){
                    Image(
                        painter = painterResource(id = R.drawable.ic_error),
                        colorFilter = ColorFilter.tint(colorScheme.error),
                        contentDescription = null,
                        modifier = Modifier.fillMaxWidth().height(24.dp),
                    )
                }
                Text(
                    splashTxt.value,
                    textAlign = TextAlign.Center,
                    fontSize = 20.sp,
                    modifier = Modifier.fillMaxWidth().wrapContentSize()
                )

            }
        }
    }


}
